package org.flexlancer.databases
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	public class Customers extends EventDispatcher
	{
		private static const TABLE:String  = "customers";
		public function Customers(target:IEventDispatcher=null)
		{
			super(target);
		}
		public static function List(condition:String = ""):Array{
			try{
				var retValue:Array = DatabaseManager.Manager.Select(TABLE,null,condition);
				for each ( var singleCust:Object in retValue ){
					singleCust.contactdetails = CustomerContactInfo.List("refid LIKE 'CUST-"+singleCust.customerid+"'");
					singleCust.vehicledetails = VehicleInfo.List("refid LIKE 'CUST-"+singleCust.customerid+"'");
				}
				return retValue;
			}catch(err:Error){
				throw err;
			}
			return new Array();
		}
		public static function Add(customerName:String, customerType:String, customerLoyalty:String ,contactDetails:Array, vehicleInfo:Array):void{
			try{//customerid INTEGER PRIMARY KEY AUTOINCREMENT","customername TEXT", "customertype TEXT", "credit REAL"]);
				DatabaseManager.Manager.Begin();
				var custObj:Object = new Object();
				custObj.customername = " '" + customerName + "' ";
				custObj.customertype =  " '" + customerType + "' ";
				custObj.customerloyalty =  " '" + customerLoyalty + "' ";
				custObj.credit =  "0.0";
				var dt:Date= new Date();
				custObj.regdate = " '"+ dt.toDateString() +"' ";
				custObj.numvehicles = vehicleInfo.length;
				
				DatabaseManager.Manager.Insert(TABLE, custObj);
				var insertID:Number = DatabaseManager.Manager.LastInsertRowID;
				
				if (contactDetails){
					var tmpContacts:Array = new Array();
					for each ( var singleContact:Object in contactDetails ){
						var contObj:Object = new Object();
						contObj.refid = " 'CUST-"+insertID+"' ";
						contObj.contacttype = " '"+singleContact.type+"' ";
						contObj.contactdetail = " '"+singleContact.detail+"' ";
						tmpContacts.push(contObj);
					}
					//CustomerContactInfo.Delete("refid LIKE 'CUST-"++"'");
					CustomerContactInfo.Add(tmpContacts);
				}
				if(vehicleInfo){
					var tmpVehicle:Array = new Array();
					for each ( var singleVehicle:Object in vehicleInfo ){
						var vehObj:Object = new Object();
						vehObj.refid = " 'CUST-"+insertID+"' ";
						vehObj.vehicletype = " '"+singleVehicle.type+"' ";
						vehObj.vehicledetail = " '"+singleVehicle.detail+"' ";
						vehObj.regnum = " '"+singleVehicle.regnum+"' ";
						tmpVehicle.push(vehObj);
					}
					//VehicleInfo.Delete("refid LIKE 'CUST-"++"'");
					VehicleInfo.Add(tmpVehicle);
				}
				DatabaseManager.Manager.Commit();
			}catch(err:Error){
				DatabaseManager.Manager.Rollback();
				throw err;
			}
		}
		public static function Update(customerid:String,customerName:String, customerType:String, customerLoyalty:String ,contactDetails:Array, vehicleInfo:Array):void{
			try{//customerid INTEGER PRIMARY KEY AUTOINCREMENT","customername TEXT", "customertype TEXT", "credit REAL"]);
				DatabaseManager.Manager.Begin();
				var custObj:Object = new Object();
				custObj.customername = " '" + customerName + "' ";
				custObj.customertype =  " '" + customerType + "' ";
				custObj.customerloyalty =  " '" + customerLoyalty + "' ";
				custObj.credit =  "0.0";
				var dt:Date= new Date();
				custObj.regdate = " '"+ dt.toDateString() +"' ";
				custObj.numvehicles = vehicleInfo.length;
				
				DatabaseManager.Manager.Update(TABLE, custObj,"customerid="+customerid);
				
				if (contactDetails){
					var tmpContacts:Array = new Array();
					for each ( var singleContact:Object in contactDetails ){
						var contObj:Object = new Object();
						contObj.refid = " 'CUST-"+customerid+"' ";
						contObj.contacttype = " '"+singleContact.type+"' ";
						contObj.contactdetail = " '"+singleContact.detail+"' ";
						tmpContacts.push(contObj);
					}
					CustomerContactInfo.Delete("refid LIKE 'CUST-"+customerid+"'");
					CustomerContactInfo.Add(tmpContacts);
				}
				if(vehicleInfo){
					var tmpVehicle:Array = new Array();
					for each ( var singleVehicle:Object in vehicleInfo ){
						var vehObj:Object = new Object();
						vehObj.refid = " 'CUST-"+customerid+"' ";
						vehObj.vehicletype = " '"+singleVehicle.type+"' ";
						vehObj.vehicledetail = " '"+singleVehicle.detail+"' ";
						vehObj.regnum = " '"+singleVehicle.regnum+"' ";
						tmpVehicle.push(vehObj);
					}
					VehicleInfo.Delete("refid LIKE 'CUST-"+customerid+"'");
					VehicleInfo.Add(tmpVehicle);
				}
				DatabaseManager.Manager.Commit();
			}catch(err:Error){
				DatabaseManager.Manager.Rollback();
				throw err;
			}
		}
		public static function Delete(condition:String = ""):void{
			try{
				DatabaseManager.Manager.Delete(TABLE,condition);
			}catch(err:Error){
				throw err;
			}
		}
	}
}